package ui.prototypes;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class CashFlowItemEditorFragment extends Fragment {

    private EditText mTxtDesc;

    private EditText mTxtValue;

    private CashFlow mCashFlow = null;

    private Button mBtnCashFlowDateButton = null;

    private SimpleDateFormat mDateFormat;

    public CashFlowItemEditorFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        //TODO access it from a single point
        mDateFormat = new SimpleDateFormat(getResources().getString(R.string.date_format));

        mCashFlow = new CashFlow();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.cash_flow_item_editor, container, false);

        mTxtDesc = (EditText) view.findViewById(R.id.cash_flow_item_description);
        mTxtValue = (EditText) view.findViewById(R.id.cash_flow_item_value);
        mBtnCashFlowDateButton = (Button) view.findViewById(R.id.cash_flow_item_button);
        mBtnCashFlowDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog(mBtnCashFlowDateButton);
            }
        });

        Intent intent = getActivity().getIntent();

        if(intent.hasExtra("cash_flow")) {
            Bundle extras = intent.getExtras();
            CashFlow cf = (CashFlow)extras.getParcelable("cash_flow");
            setCashFlow(cf);
        }else {
            //Populate data field
            Calendar c = Calendar.getInstance();
            populateDateView(c);
        }

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.edit_item, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {
        case R.id.edit_item_save:
            actionSave();
            break;
        case R.id.edit_item_cancel:
            actionCancel();
            break;
        }

        return true;
    }

    public void setCashFlow(CashFlow cf) {
        mCashFlow = cf;

        populateUI(mCashFlow);

    }

    public CashFlow loadCashFlow() {

        String strDescription = mTxtDesc.getText().toString();
        String strValue = mTxtValue.getText().toString();

        try {
            mCashFlow.setValue(Double.parseDouble(strValue));
        } catch (NumberFormatException nfe) {
            //TODO
        }

        mCashFlow.setDescription(strDescription);

        return mCashFlow;
    }

    private void populateUI(CashFlow cf) {
        mTxtDesc.setText(cf.getDescription());
        mTxtValue.setText(String.format(getResources().getString(R.string.real_number_format), cf.getValue()));

        populateDateView(cf.getDate());
    }

    private void actionSave() {
        loadCashFlow();
        mCashFlow.store(getActivity());
        getActivity().finish();
    }

    private void actionCancel() {
        getActivity().finish();
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment(mCashFlow.getDate(), mDateListener);
        newFragment.show(getFragmentManager(), "datePicker");
    }

    private void populateDateView(Calendar c) {
        mBtnCashFlowDateButton.setText(mDateFormat.format(c.getTime()));
    }

    private final DatePickerDialog.OnDateSetListener mDateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {

            Calendar c = Calendar.getInstance();

            c.set(Calendar.YEAR,  year);
            c.set(Calendar.MONTH, monthOfYear);
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            mCashFlow.setYear(c.get(Calendar.YEAR));
            mCashFlow.setMonth(c.get(Calendar.MONTH));
            mCashFlow.setDayOfMonth(c.get(Calendar.DAY_OF_MONTH));

            populateDateView(c);
        }
    };
}
